/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AreaDeDesenho;

import Formas.Forma;
import Helpers.EFormas;
import Helpers.ETransformacoes;
import Helpers.Transformer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 *
 * @author aluno
 */
public class PainelDeDesenho extends JPanel implements ImageObserver {

    private static EFormas controleDeFormas;
    private static Color corDoPincel;
    public LinkedBlockingQueue<Forma> filaFormas;
    public Stack<Transformer> filaTransf;
    public BufferedImage imagem;
    public Stack<Transformer> refazer;

    public void addForma(Forma f)
    {
       filaFormas.add(f);
    }

    public Forma getForma(Forma f)
    {
      return filaFormas.peek();
    }

     public void addTransf(Transformer e)
    {
       filaTransf.add(e);
    }

    public Transformer getTranf()
    {
      return filaTransf.peek();
    }
    /**
     * @return the corDoPincel
     */
    public static Color getCorDoPincel() {
        return corDoPincel;
    }

     /**
     * @param aCorDoPincel the corDoPincel to set
     */
    public static void setCorDoPincel(Color aCorDoPincel) {
        corDoPincel = aCorDoPincel;
        //this.getGraphics().setColor(corDoPincel);
    }

    public PainelDeDesenho() {
        super();
        setSize(new Dimension(640, 480));
        setVisible(true);
        Border borda = BorderFactory.createLineBorder(Color.BLACK, 1);
        this.setBorder(borda);
        this.setBackground(Color.WHITE);
        controleDeFormas = EFormas.NONE;
        filaFormas = new LinkedBlockingQueue<Forma>();
        filaTransf = new Stack<Transformer>();
        Transformer f = new Transformer();
        f.setTransformacao(ETransformacoes.None);
        filaTransf.add(f);
        refazer = new Stack<Transformer>();
    }

    @Override
    public void paint(Graphics g) {

        super.paint(g);

        for(Transformer e: filaTransf)
              Transformar((Graphics2D) g,e);

        if(imagem!=null)
            g.drawImage(imagem, 0, 0, this);

        if(filaFormas != null)
        {
            for(Forma f : filaFormas)
            {
              f.drawForma(g);
           }
        }
    }

    @Override
    public void repaint() {
        
        if(filaTransf != null)
        for(Transformer e: filaTransf)
             Transformar((Graphics2D) this.getGraphics(), e);
        super.repaint();

        if(imagem!=null)
            this.getGraphics().drawImage(imagem, 0, 0, this);

        if(filaFormas != null)
        {
            for(Forma f : filaFormas)
            {
                f.drawForma(this.getGraphics());
            }
        }
    }


    public static void setFormaAtual(EFormas forma) {
        controleDeFormas = forma;
    }

    public static EFormas getFormaAtual() {
        return controleDeFormas;
    }

    public void SalvarImagem(String filename) throws IOException {
        BufferedImage image = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics g = image.createGraphics();
      
        this.paint(g);
/*
        if(filaTransf != null)
        for(Transformer e: filaTransf)
             Transformar((Graphics2D) this.getGraphics(), e);
*/
        if(imagem!=null)
            g.drawImage(imagem, 0, 0, this);
        
        for(Forma f: filaFormas)
        {          
           f.drawForma(g);
        }


        File f = new File(filename);
        ImageIO.write(image, "png", f);
    }

    public void AbrirImagem(String path)
    {
       File f = new File(path);
       BufferedImage image = null;//new BufferedImage(this.getWidth(),
        try {
            image = ImageIO.read(f);
        } catch (IOException ex) {
            Logger.getLogger(PainelDeDesenho.class.getName()).log(Level.SEVERE, null, ex);
        }
       imagem = image;
       this.getGraphics().drawImage(image,0,0,this);
    }

    public void Transformar(Graphics2D g, Transformer e)
    {
        if(e!=null)
        {
            switch(e.getTransformacao())
            {
                case Rotate:
                    g.rotate(e.getAngulo());
                    break;
                case Scale:
                    g.scale(e.getX(), e.getY());
                    break;
                case Translate:
                    g.translate(e.getX(), e.getY());
                break;
                case Sheare:
                    g.shear(e.getX(), e.getY());
                break;
            }
        }
    }

}
